package com.gomore.opple.module.order;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

import com.gomore.opple.R;
import com.gomore.opple.common.GlobalConstant;
import com.gomore.opple.module.BaseFragment;
import com.gomore.opple.module.IntentStart;
import com.gomore.opple.module.order.adapter.OrderAdapter;
import com.gomore.opple.service.ChangeOrderStateClickListener;
import com.gomore.opple.utils.DialogUtils;
import com.gomore.opple.web.cgform.order.entity.TOOrderEntity;
import com.gomore.opple.widgets.PullBaseView;
import com.gomore.opple.widgets.PullRecyclerView;
import com.gomore.opple.widgets.XCRoundRectImageView;
import com.gomore.opple.widgets.adapter.MultiItemTypeAdapter;

import butterknife.Bind;
import butterknife.OnClick;


/**
 * Created by robot
 */
public class OrderFragment extends BaseFragment implements OrderContract.View,
        PullBaseView.OnRefreshListener, ChangeOrderStateClickListener {

    @Bind(R.id.back)
    LinearLayout back;
    @Bind(R.id.spinner_cash_type)
    Spinner spinner_cash_type;
    @Bind(R.id.recyclerView)
    PullRecyclerView recyclerView;
    @Bind(R.id.empty_view)
    View empty_view;

    private OrderContract.Presenter mPresenter;
    private OrderAdapter orderAdapter;
    private String state;

    public static OrderFragment getInstance() {
        return new OrderFragment();
    }

    @Override
    protected int getLayoutView() {
        return R.layout.fragment_order;
    }

    @Override
    protected void initalizeData() {
        mPresenter.queryOrder(false, state);
    }

    @Override
    protected void initalizeViews() {
        mPresenter.prepareInitData();
        orderAdapter = new OrderAdapter(getActivity(), getActivity(), R.layout.item_order, mPresenter.getData());
        recyclerView.setLayoutManager(new LinearLayoutManager(OrderFragment.this.getActivity()));
        recyclerView.setAdapter(orderAdapter);
        recyclerView.setOnRefreshListener(this);
        orderAdapter.setChangeOrderStateClickListener(this);
        orderAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                TOOrderEntity toOrderEntity = mPresenter.getData().get(position).getOrder();
                IntentStart.getInstance().startOrderPayActivity(getActivity(), toOrderEntity.getOrderNumber(), toOrderEntity.getOrderState());
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });
        //适配器
        ArrayAdapter spinner_cash_type_adapter = new ArrayAdapter(getActivity(), android.R.layout.simple_spinner_item, GlobalConstant.OrderState.OrderStateList());
        //设置样式
        spinner_cash_type_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //加载适配器
        spinner_cash_type.setAdapter(spinner_cash_type_adapter);
        spinner_cash_type.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                TextView tv = (TextView) view;
                tv.setTextColor(getResources().getColor(R.color.gray_text));
                tv.setTextSize(18);
                String name = GlobalConstant.OrderState.OrderStateList().get(position);
                state = GlobalConstant.OrderState.getStateCodeByName(name);
                mPresenter.queryOrder(false, state);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }


    @Override
    public void setPresenter(OrderContract.Presenter presenter) {
        mPresenter = presenter;
    }

    @Override
    public void showProgressDialog() {
        DialogUtils.showLoadingDialog(getActivity(), getResources().getString(R.string.loading));
    }

    @Override
    public void hideProgressDialog() {
        DialogUtils.closeLoadingDialog();
    }

    @Override
    public void showMessage(String msg) {
        showToast(msg);
    }

    @Override
    public void showMessage(int msgId) {
        showToast(msgId);
    }

    /**
     * 加载完毕
     */
    @Override
    public void showLoadMoreCompleted() {
        recyclerView.onFooterRefreshComplete();
    }

    /**
     * 刷新完毕
     */
    @Override
    public void showRefreshCompleted() {
        recyclerView.onHeaderRefreshComplete();
    }

    @Override
    public void showContentView() {
        orderAdapter.notifyDataSetChanged();
        if (mPresenter.getData().size() == 0) {
            recyclerView.setVisibility(View.GONE);
            empty_view.setVisibility(View.VISIBLE);
        } else {
            recyclerView.setVisibility(View.VISIBLE);
            empty_view.setVisibility(View.GONE);
        }
    }

    @Override
    public void reflashData() {
        mPresenter.queryOrder(false, state);
    }

    @OnClick({R.id.back})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                getActivity().finish();
                break;
        }
    }

    @Override
    public void onHeaderRefresh(PullBaseView view) {
        mPresenter.queryOrder(false, state);
    }

    @Override
    public void onFooterRefresh(PullBaseView view) {
        mPresenter.queryOrder(true, state);
    }

    @Override
    public void changeOrderState(String orderNumber) {
        mPresenter.changeOrderState(orderNumber);
    }
}
